using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Soyuz5.Utility
{
    public static class String
    {
        private static Regex _properCaseRegEx = new Regex(
            "[^\\s]+",
            RegexOptions.IgnoreCase
            | RegexOptions.CultureInvariant
            | RegexOptions.IgnorePatternWhitespace
            | RegexOptions.Compiled
            );

        // word match only alpha chars
        private static Regex _wordRegEx = new Regex(
            @"[a-z\']+",
            RegexOptions.IgnoreCase
            | RegexOptions.CultureInvariant
            | RegexOptions.IgnorePatternWhitespace
            | RegexOptions.Compiled
            );

        private static string ProperCaseMatchEvaluator(Match m)
        {
            string word = m.Value;

            if (!_wordRegEx.Match(word).Value.Equals(word))
            {
                return word;
            }

            // Do basic case
            switch (word.ToLower())
            {
                // Common name exceptions
                case "mcdonald": word = "McDonald"; break;
                case "macdonald": word = "McDonald"; break;
                case "macneil": word = "MacNeil"; break;
                case "mcneil": word = "McNeil"; break;
                case "otool": word = "O'Tool"; break;
                case "oconner": word = "O'Conner"; break;

                // short word 
                case "to":
                case "of":
                case "the":
                case "by":
                case "this":
                case "is":
                case "from":
                case "a":
                    word = word.ToLower();
                    break;

                default:
                    word = word[0].ToString().ToUpper() + word.Substring(1).ToLower();
                    break;
            }
            return word;
        }
        /// <summary>
        /// Convert string to proper case ie (Old McDonald Had a Farm)
        /// </summary>
        /// <param name="Text"></param>
        /// <returns></returns>
        public static string ProperCase(string Text)
        {
            string result = _properCaseRegEx.Replace(Text, new MatchEvaluator(ProperCaseMatchEvaluator));

            // First char should always be uppercase
            if (result.Length > 0)
                result = result[0].ToString().ToUpper() + result.Substring(1);

            return result;
        }

        /// <summary>
        /// Encode text as html
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ToHtml(string text)
        {
            System.IO.StringWriter writer = new System.IO.StringWriter();
            System.Web.HttpUtility.HtmlEncode(text, writer);
            return writer.ToString().Replace(Environment.NewLine, "<br />");
        }

        /// <summary>
        /// Remove all html
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string StripHtml(string text)
        {
            string HTML_TAG_PATTERN = "<.*?>";
            return Regex.Replace(text, HTML_TAG_PATTERN, string.Empty);
        }

        /// <summary>
        /// Convert camel case to sentance ie HelloWorld => Hello World
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string CamelCaseToSentance(string text)
        {
            return Regex.Replace(text, "([A-Z]{1,2}|[0-9]+)", " $1").TrimStart();
        }

        /// <summary>
        /// Get acronym of given text ie Hello World => HW
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Acronym(string text)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Match m in Regex.Matches(text, "\\b[a-zA-Z]"))
            {
                sb.Append(m.Value);
            }
            return sb.ToString();
        }

    }
}
